﻿//-----------------------------------------------------------------------
// <copyright file="Common.cs" company="Robert MacLean">
//     This file is released under the LGPL. For more information see 
//     http://gallery2.codeplex.com.
//     Originally created by Robert MacLean http://www.sadev.co.za
// </copyright>
//-----------------------------------------------------------------------

namespace SADev.Gallery2.Protocol
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;

    /// <summary>
    /// Common methods for the G2 protocol
    /// </summary>
    internal static class Common
    {
        /// <summary>
        /// Gets the base URL to append to the server+path
        /// </summary>
        public static string Url
        {
            get
            {
                return "/main.php?g2_controller=remote:GalleryRemote";
            }
        }

        /// <summary>
        /// converts to the commands to a string in the right format.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <returns>A string of the commands</returns>
        public static string ToPostString(this Dictionary<string, string> commands)
        {
            string result = string.Empty;
            foreach (KeyValuePair<string, string> command in commands)
            {
                result += String.Format(CultureInfo.CurrentCulture, "&g2_form[{0}]={1}", command.Key, command.Value);
            }

            return result;
        }

        /// <summary>
        /// converts a boolean to the G2 yes/no value
        /// </summary>
        /// <param name="value">if set to <c>true</c> [value].</param>
        /// <returns>"yes" if true and "no" if false.</returns>
        public static string ToG2Value(this bool value)
        {
            if (value)
            {
                return "yes";
            }
            else
            {
                return "no";
            }
        }

        /// <summary>
        /// Gets the string value.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetStringValue(this Dictionary<string, string> values, string name)
        {
            if (values.ContainsKey(name))
            {
                return values[name];
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the int value.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static int GetIntValue(this Dictionary<string, string> values, string name)
        {
            string stringValue = values.GetStringValue(name);
            int result = 0;
            if (!string.IsNullOrEmpty(name))
            {
                if (!Int32.TryParse(stringValue, NumberStyles.Any, CultureInfo.CurrentCulture, out result))
                {
                    result = 0;
                }
            }
            return result;
        }
    }
}